/**
$Id: pion-glue.hpp 3096 2010-07-06 07:21:14Z Bear $

Copyright (c) 2010 Nuwa Information Co., Ltd, and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Neither the name of Nuwa Information nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

$Author: Bear $
$Date: 2010-07-06 15:21:14 +0800 (星期二, 06 七月 2010) $
$Revision: 3096 $
*/
#ifndef __WSGISERVICE_GLUE__
#define __WSGISERVICE_GLUE__

#include <pion/net/HTTPResponseWriter.hpp>

namespace pion {		// begin namespace pion
namespace plugins {		// begin namespace plugins

/**
A simple handler to be a handler after sendChunk.
*/
class WsgiSendHandler
{
public:

 // For boost::bind.
 typedef void result_type;

 /**
 Constructor.
 */
 WsgiSendHandler() : sentBytes(0)
 {
 }

 /**
 Handle.
 @param error Error code.
 @param bytes Bytes transferred.
 */
 void operator()(const boost::system::error_code& error,
					            std::size_t bytes)
 {
  sentBytes += bytes;
 }

public:

 /// Sent bytes.
 unsigned long	sentBytes;

};

class WsgiService;

/**
Context to pass between WSGI module implementation and pion service.
*/
struct WsgiContext
{
 /// Service itself.
 WsgiService* service;

 /// TCPConnection.
 pion::net::TCPConnectionPtr connection;

 /// HTTPRequest.
 pion::net::HTTPRequestPtr request;

 /// HTTPResponse.
 pion::net::HTTPResponseWriterPtr response;

 /// Private data to stores python Request object.
 void *data;

 /// Send handler.
 WsgiSendHandler sendHandler;

 /// Sent bytes.
 unsigned long sentBytes;

 /**
 Constructor.
 @param _srv WsgiService.
 @param _req HTTPRequestPtr.
 @param _conn TCPConnectionPtr.
 @param _res HTTPResponseWriterPtr.
 */
 explicit WsgiContext(WsgiService* _srv, 
                      pion::net::HTTPRequestPtr& _req, 
                      pion::net::TCPConnectionPtr& _conn,
                      pion::net::HTTPResponseWriterPtr& _res)
  : service(_srv), request(_req), connection(_conn), response(_res),
    data(NULL), sentBytes(0)
 {
 }
};

}	// end namespace plugins
}	// end namespace pion

#endif
